﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Security;
using System.Runtime.InteropServices;

namespace adHoc_8
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
                       
        }

        private void Create_Click(object sender, RoutedEventArgs e)
        {
            String txtPassword = PasswordBox.Password;

            SecureString objSecureString = new SecureString();
            char[] passwordChar = txtPassword.ToCharArray();
            foreach (char c in passwordChar)
                objSecureString.AppendChar(c);
            objSecureString.MakeReadOnly();//Notice at the end that the MakeReadOnly command prevents the SecureString to be edited any further.
            
            //Reading a SecureString is more complicated. There is no simple ToString method, which is also intended to keep the data secure. To read the data C# developers must access the data in memory directly. Luckily the .NET Framework makes it fairly simple:
            IntPtr stringPointer = Marshal.SecureStringToBSTR(objSecureString);
            string normalString = Marshal.PtrToStringBSTR(stringPointer);//Original Password text

            if (SSID.Text == "" || normalString == "")
            {
                Remark.Text = "Please enter a valid SSID and Key";

            }
            else
            {
                try
                {
                    //Set the network SSID and Key.
                    string commandToExecute = @"netsh wlan set hostednetwork mode=allow ssid=" + SSID.Text + " key=" + normalString;
                    Process.Start(@"cmd", @"/c " + commandToExecute);

                    //Start the hostednetwork
                    string commandToExecute2 = @"netsh wlan start hostednetwork";
                    Process.Start(@"cmd", @"/c " + commandToExecute2);
                    Remark.Text = "Succesfully created ad-Hoc Connection.";
                }
                catch
                {
                    Remark.Text = "Cannot Create ad-Hoc Connection.";
                }
            }
        }

        private void Stop_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string commandToExecute = @"netsh wlan stop hostednetwork";
                Process.Start(@"cmd", @"/c " + commandToExecute);
                Remark.Text = "Succesfully terminated ad-Hoc Connection.";
            }
            catch{
                Remark.Text = "Cannot terminate ad-Hoc Connection.";
            }
        }

       

        private void Help_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("adHoc 8 Version 0.1.2013.0419 alpha\nCreated By: Sanjan Piya\nFor Comments and Queries: eakstickpiya@live.com\nTwitter: twitter.com/sanjanpiya");
            
        }

    }
}
